/**
 * name:        个人中心-收货地址
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */
$(function(){
    // $(".address_wrap").append('<div class="none" style="line-height:200px; text-align:center; color:#666; font-size:20px;">加载中</div>');
    var is = ''
    var site=''
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token=''
        isLogin();      
        getToken();     // 获取/验证token
        if( token!='' && token!=null){
            $('.address_wrap').append('<div class="none" style="width:100%;height:300px;background: #fff url(../../img/base/loading.gif) center center no-repeat;"></div>');
            getAddress();
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){
            $('.nike_name').html(strs[1]); 
            token=strs[6];
        }
    }

    // 获取收货地址
    function getAddress(){
        $.ajax({
            type:'post',
            url:new_url+'shop/member/getShippingAddressList',
            dataType:'json',
            data:{token:token},  
            success:function(e){
                $('.none').remove();
                if(e.data.length=='0'){ 
                    $('.site_list_list').remove(); 
                    $('.address_bottom').css('display','block'); 
                    $('.address_bottom p').remove(); 
                }else{ 
                    $('.site_list_no').remove(); 
                    for (var i=0; i<e.data.length; i++){ 
                        if(e.data[i].isDefault == 1){ 
                            e.data[i].isDefaultTxt='默认收货地址';
                        }else{
                            e.data[i].isDefaultTxt='设为默认地址';
                        }
                        var address = $("#address").html();
                        var addressListcont=template(
                            address, {
                                addressList: [{
                                    addressee:e.data[i].addressee,
                                    phone:e.data[i].phone,
                                    address:e.data[i].address,
                                    areaInfo:e.data[i].areaInfo,
                                    isDefault:e.data[i].isDefault,
                                    isDefaultTxt:e.data[i].isDefaultTxt,
                                    addressId:e.data[i].addressId,
                                    provinceId:e.data[i].provinceId,
                                    cityId:e.data[i].cityId,
                                    areaId:e.data[i].areaId,
                                }] 
                            } 
                        ); 
                        $('.address_wrap ul').append(addressListcont); 
                        $('.address_bottom').css('display','block'); 
                        $('.address_bottom p').text('（共'+e.data.length+'个收货地址，最多可以保存9个地址）'); 
                    } 
                    $('.address_wrap ul li').each(function(index,t){ 
                        is=$(t).find('.address_set_default').attr('data-isdefault'); 
                        if(is=='1'){ 
                            $(t).addClass('active'); 
                            $(t).find('.delete_address').remove(); 
                            $(t).find('.edit_address').css({ 
                                'float':'right',
                                'margin-right':'10px'
                            }); 
                        } 
                        $('.address_wrap ul li').click(function(){ 
                            $('.address_wrap ul li').removeClass('active'); 
                            $(this).addClass('active'); 
                            site=$(this).attr('data-addres-id'); 
                        }); 
                        site=$('.address_wrap ul li.active').attr('data-addres-id'); 
                    }); 
                    var num=0; 
                    $(".address_wrap ul li").each(function(){ 
                        num = num +1; 
                    }); 
                    if(num>=9){ 
                        $(".add_address").text('不能添加').removeClass('add_address').addClass('add_address1'); 
                    } 
                } 
                isDefault(token,URL); 
                deleteAddress(token,URL); 
                editAddress(); 
                addAddress(); 
            }   
        }); 
    } 

    // 编辑收货地址 
    function editAddress(){ 
        $(".edit_address").click(function(){ 
            var addressId = $(this).parents().find("li").attr('data-addres-id'); 
            var addressee = $(this).parents().find(".address_tit .address_name").html(); 
            var areaId = $(this).attr('data-areaId'); 
            var cityId = $(this).attr('data-cityId'); 
            var provinceId = $(this).attr('data-provinceId'); 
            var areaInfo = $(this).parents().find(".address_info").attr('data-area-info'); 
            var address = $(this).parents().find(".address_info").attr('data-address'); 
            var phone = $(this).parents().find(".address_tit .address_phone").html(); 
            var isDefault = 0; 
            editAddressPop(addressId,addressee,areaId,cityId,provinceId,areaInfo,address,phone,isDefault,token,URL); 
        }); 
    } 

    // 新增收货地址 
    function addAddress(){ 
        $(".add_address").click(function(){ 
            addAddressPop(token,URL); 
        }); 
    } 

    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' }); 
        $.ajax({ 
            type:'post', 
            url:new_url+'shop/login/logOut', 
            dataType:'json', 
            data:'', 
            success:function(data){ 
                tc(data.message); 
            } 
        }); 
        setTimeout(function(){ 
            window.location.href='/index.html'; 
        },1100); 
    }); 
    

}); 